package cn.iocoder.yudao.module.hs.controller.admin.material;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.hs.controller.admin.material.vo.MaterialModeRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.material.vo.MaterialPageReqVO;
import cn.iocoder.yudao.module.hs.controller.admin.material.vo.MaterialRespVO;
import cn.iocoder.yudao.module.hs.controller.admin.material.vo.MaterialSaveReqVO;
import cn.iocoder.yudao.module.hs.dal.dataobject.material.MaterialDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.qcorderdetail.QcOrderDetailDO;
import cn.iocoder.yudao.module.hs.service.material.MaterialService;
import cn.iocoder.yudao.module.hs.service.qcorderdetail.QcOrderDetailService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 物料信息")
@RestController
@RequestMapping("/hs/material")
@Validated
public class MaterialController {

    @Resource
    private MaterialService materialService;

    @Resource
    private QcOrderDetailService qcOrderDetailService;
    @Autowired
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建物料信息")
    @PreAuthorize("@ss.hasPermission('hs:material:create')")
    public CommonResult<Long> createMaterial(@Valid @RequestBody MaterialSaveReqVO createReqVO) {
        return success(materialService.createMaterial(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料信息")
    @PreAuthorize("@ss.hasPermission('hs:material:update')")
    public CommonResult<Boolean> updateMaterial(@Valid @RequestBody MaterialSaveReqVO updateReqVO) {
        materialService.updateMaterial(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hs:material:delete')")
    public CommonResult<Boolean> deleteMaterial(@RequestParam("id") Long id) {
        materialService.deleteMaterial(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hs:material:query')")
    public CommonResult<MaterialRespVO> getMaterial(@RequestParam("id") Long id) {
        MaterialDO material = materialService.getMaterial(id);
        return success(BeanUtils.toBean(material, MaterialRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料信息分页")
    @PreAuthorize("@ss.hasPermission('hs:material:query')")
    public CommonResult<PageResult<MaterialRespVO>> getMaterialPage(@Valid MaterialPageReqVO pageReqVO) {
        PageResult<MaterialDO> pageResult = materialService.getMaterialPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialRespVO.class));
    }

    @GetMapping("/qc-page")
    @Operation(summary = "获得物料质检信息分页")
    @PreAuthorize("@ss.hasPermission('hs:material:query')")
    public CommonResult<PageResult<MaterialRespVO>> getMaterialQcPage(@Valid MaterialPageReqVO pageReqVO) {
        AdminUserRespDTO user = adminUserApi.getUser(getLoginUserId());
        if (user != null) {
            pageReqVO.setWarehouseId(user.getWarehouseId());
        }
        PageResult<MaterialDO> pageResult = materialService.getMaterialPage(pageReqVO);
        PageResult<MaterialRespVO> pageRespResult = BeanUtils.toBean(pageResult, MaterialRespVO.class);
        if (CollUtil.isNotEmpty(pageRespResult.getList())) {
            pageRespResult.getList().forEach(materialRespVO -> {
                QcOrderDetailDO qcOrderDetail = qcOrderDetailService.getQcOrderDetail(materialRespVO.getCode());
                if (qcOrderDetail != null) {
                    materialRespVO.setInfoCode(qcOrderDetail.getInfoCode());
                    materialRespVO.setIsEntry(qcOrderDetail.getIsEntry());
                }
            });
        }
        return success(pageRespResult);
    }

    @GetMapping("/mode-page")
    @Operation(summary = "获得物料机型分页")
    @PreAuthorize("@ss.hasPermission('hs:material:query')")
    public CommonResult<PageResult<MaterialModeRespVO>> getMaterialModePage(@Valid MaterialPageReqVO pageReqVO) {
        PageResult<MaterialModeRespVO> pageResult = materialService.getMaterialModePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialModeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料信息 Excel")
    @PreAuthorize("@ss.hasPermission('hs:material:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialExcel(@Valid MaterialPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialDO> list = materialService.getMaterialPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料信息.xls", "数据", MaterialRespVO.class,
                        BeanUtils.toBean(list, MaterialRespVO.class));
    }

}